package jp.co.dailyReport.expense;

import java.sql.SQLException;
import org.apache.log4j.Logger;
import jp.co.dailyReport.ExecuterManager;
import jp.co.dailyReport.ConnectionFactory;
import jp.co.dailyReport.DataUnavailableException;
import jp.co.dailyReport.expense.sql.ExpenseManufacturingLoadExecuter;
import jp.co.dailyReport.expense.sql.ExpenseManufacturingCountExecuter;

import jp.co.dailyReport.staff.Staff;
//import jp.co.dailyReport.PermissionManager;

/**
 * 仕掛計上結果情報管理クラス
 */
public class ExpenseManufacturingManager {

	private static Logger log = Logger.getLogger(ExpenseManufacturingManager.class);

	/**
	 * 仕掛計上結果情報を取得します。
	 * 
	 * @param expenseManufacturingCondition 仕掛計上条件オブジェクト
	 * @return 仕掛計上結果オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static ExpenseManufacturingResult[] load(ExpenseManufacturingCondition expenseManufacturingCondition)
	 throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		ExpenseManufacturingLoadExecuter expenseManufacturingLoadExecuter = new ExpenseManufacturingLoadExecuter(expenseManufacturingCondition);
		Object objs[] = (Object[])null;

		try {
			objs = ExecuterManager.execute(connectionFactory, expenseManufacturingLoadExecuter);
		} catch(SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}

		if(objs == null || objs.length == 0) {
			return new ExpenseManufacturingResult[0];
		} else {
			return (ExpenseManufacturingResult[])objs;
		}
	}

	/**
	 * 仕掛計上結果件数を取得します。
	 * 
	 * @param expenseManufacturingCondition 仕掛計上条件オブジェクト
	 * @return 仕掛計上結果件数
	 * @throws DataUnavailableException
	 */
	public static int getExpenseManufacturingResultCount(ExpenseManufacturingCondition expenseManufacturingCondition)
	 throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		ExpenseManufacturingCountExecuter expenseManufacturingCountExecuter = new ExpenseManufacturingCountExecuter(expenseManufacturingCondition);
		Object objs[] = null;

		try {
			objs = ExecuterManager.execute(connectionFactory, expenseManufacturingCountExecuter);
		} catch(SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}

		if(objs == null || objs.length == 0) {
			return 0;
		} else {
			return ((Integer[])objs)[0].intValue();
		}
	}

	/**
	 * 社員の権限で仕掛計上結果情報を取得します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param expenseManufacturingCondition 仕掛計上条件オブジェクト
	 * @return 仕掛計上結果オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static ExpenseManufacturingResult[] load(Staff staff,ExpenseManufacturingCondition expenseManufacturingCondition)
	 throws DataUnavailableException {

//		if(PermissionManager.hasPermission(staff,PermissionManager.HOURS_WORKEDFOREVERYPROJECT,PermissionManager.METHOD_READ)) {
			return load(expenseManufacturingCondition);
//		}else{
//			return new ExpenseManufacturingResult[0];
//		}
	}

	/**
	 * 社員の権限で仕掛計上結果件数を取得します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param expenseManufacturingCondition 仕掛計上条件オブジェクト
	 * @return 仕掛計上結果件数
	 * @throws DataUnavailableException
	 */
	  public static int getExpenseManufacturingResultCount(Staff staff,ExpenseManufacturingCondition expenseManufacturingCondition) 
		  throws DataUnavailableException {

//			if(PermissionManager.hasPermission(staff,PermissionManager.HOURS_WORKEDFOREVERYPROJECT,PermissionManager.METHOD_READ)) {
			  return getExpenseManufacturingResultCount(expenseManufacturingCondition);
//			}else{
//			  return 0;
//			}
	  }


}
